package com.hspedu.encap;

public class Encapsulation01 {
    public static void main(String[] args) {
        Person person = new Person("jackccscf",303,30000);
//        person.setName();
//        person.setAge();
//        person.setSalary();
        System.out.println(person.Info());


    }
}

class Person{
    public String name;
    private int age;
    private double salary;

    public Person(String name, int age, double salary) {
        //这样用构造器初始化成员变量就没办法防护了，即private 就不起作用了
        //this.name = name;
        //this.age = age;
        //this.salary = salary;
        //不过我们可以把set方法写在构造器里面
        setName(name);
        setAge(age);
        setSalary(salary);
    }

    public void setName(String name) {
        if(name.length() >= 2 && name.length() <= 6)
            this.name = name;
        else{
            System.out.println("设置的名字不对，需要在（2-6），给定默认名字“无名人");
            this.name = "无名人";
        }

    }

    public void setAge(int age) {
        if(age >= 1 && age <=120)
            this.age = age;
        else{
            System.out.println("设置的年龄不对，需要在（1-120），给定默认年龄18");
            this.age = 18;
        }
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getSalary() {
        return salary;
    }

    public String Info(){
        System.out.println("=========信息为==========");
        return "信息为 names " + name + " age=" + age + " 薪水" + salary;
    }
}
